﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Brain;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Brain.Dev;

namespace Demos
{
    public class AnimateSprite : DemoScreen
    {
        public Sprite RectSprite;

        public AnimateSprite()
        {
            RectSprite = new Sprite();
            RectSprite.BoundsRectangle = new Rectangle(0, 0, 100, 100);
            RectSprite.BoundsRectangleColor = Color.White;
            this.AddComponent(RectSprite);
            RectSprite.Rendering = SpriteRendering.Rendered;

            // A Little info about the Animation System
            // -> You can input the Frames on ANY frame order. Calling Optimize will order them and make the system work(look below)
            // -> You can see that the last frame(150) doesn't have the same position as the first one, but the 
            // animation syncs perfectly. That's because of the LoopRestartTime variable, that gives the Animation
            // some time to interpolate to the beginning, causing smooth animation.
            // -> If you just want to animate a part of the Sprite, like the Position,
            // you can use a class like what it's used here, Animation_Vector2.
            // Now, if you want to animate MORE parts of the Sprite, like text, rotation,
            // and pretty much everything the sprite have, you can use
            // the Animation_Sprite, which is used on the 2nd sample of animating Sprites.
            Animation_Vector2 anim = new Animation_Vector2(SetValue);

            anim.LoopRestartTime = 50;
            anim.Loop = true;
            anim.SubmitFrame(0, Vector2.Zero);
            anim.SubmitFrame(50, new Vector2(0, 100));
            anim.SubmitFrame(100, new Vector2(50, 50));
            anim.SubmitFrame(150, new Vector2(100, 100));

            // Calling Optimize will:
            // -> Order the List of KeyFrames. This take some time from the loading, but this is a precious
            // time. With this, the time of Updating the animation drops drastically. Without the animations in order,
            // every update of the animation it would have to find by looping over ALL FRAMES to see which one is 
            // the last one, the next one, to be able to loop. This can just destroy perfomance.
            // -> Transform the List of KeyFrames that helps storing the just-created keyframes
            // to an Array of KeyFrames. This happens because array indexing works faster in most devices.
            // Lists use Generics and also other stuff that I know but don't know how to explain that makes
            // it slower to read(my tests on the Xbox 360 showed that the array is twice as faster as a list)
            anim.Optimize();

            // Then, we add the Animation to the Sprite's Timeline.
            // If you look at the AnimationTimeLine's source, you'll see that it only really initializes itself
            // when you add an Animation to it(just F12 on the below method and you will understand)
            RectSprite.TimeLine.AddAnimation(anim);

            // Add a Debug Variable, so we can see the Current Frame of the Engine's Animation Manager
            // by pressing F3 during gameplay
            DebugUtil.Add(this.ReturnValue);
        }
        string ReturnValue()
        {
            return "Frame: " + Engine.Instance.AnimationManager.CurrentFrame.ToString();
        }

        void SetValue(Vector2 vec)
        {
            RectSprite.Position = vec;
        }

        public override void Update()
        {
            base.Update();

            if (!Engine.Instance.AnimationManager.Playing)
            {
                Engine.Instance.AnimationManager.Play();
            }
        }

    }
}
